﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Documents;
using System.Windows;

namespace ChromaCode.Models
{
    /// <summary>
    /// Element of a text range that we will be highlighting
    /// </summary>
    internal class HighlightElement
    {
        #region Public

        /// <summary>
        /// Create a text element to highlight
        /// </summary>
        public HighlightElement()
        {
        }

        /// <summary>
        /// Create a text element to highlight
        /// </summary>
        /// <param name="start">The starting point for the text element</param>
        /// <param name="end">The ending point for the text element</param>
        /// <param name="ruleToApply">The rule to apply</param>
        public HighlightElement(TextPointer start, TextPointer end, Rule ruleToApply)
        {
            Start = start;
            End = end;
            RuleToApply = ruleToApply;
        }

        /// <summary>
        /// The starting point for this text element
        /// </summary>
        public TextPointer Start
        {
            get;
            set;
        }

        /// <summary>
        /// The ending point for this text element
        /// </summary>
        public TextPointer End
        {
            get;
            set;
        }

        /// <summary>
        /// The rule to apply to this text element
        /// </summary>
        public Rule RuleToApply
        {
            get;
            set;
        }

        /// <summary>
        /// Apply this rule
        /// </summary>
        public void ApplyRule()
        {
            if ((Start != null) && (End != null) && (RuleToApply != null))
            {
                TextRange highlightRange = new TextRange(Start, End);
                
                // Apply bold
                if (RuleToApply.Bold)
                {
                    highlightRange.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
                }
                else
                {
                    highlightRange.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Normal);
                }

                // Apply italic
                if (RuleToApply.Italic)
                {
                    highlightRange.ApplyPropertyValue(TextElement.FontStyleProperty, FontStyles.Italic);
                }
                else
                {
                    highlightRange.ApplyPropertyValue(TextElement.FontStyleProperty, FontStyles.Normal);
                }

                // Apply underline
                if (RuleToApply.Underline)
                {
                    highlightRange.ApplyPropertyValue(Inline.TextDecorationsProperty, TextDecorations.Underline);
                }
                else
                {
                    highlightRange.ApplyPropertyValue(Inline.TextDecorationsProperty, null);
                }

                // Forcolor
                highlightRange.ApplyPropertyValue(TextElement.ForegroundProperty, RuleToApply.Color);
            }
        }

        #endregion Public
    }
}
